package com.example.a28796.beautiful.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.a28796.beautiful.QuanziTwoActivity;
import com.example.a28796.beautiful.R;
import com.example.a28796.beautiful.SearchActivity;
import com.example.a28796.beautiful.styAdapter.ButtonAdapter;
import com.example.a28796.beautiful.styAdapter.ShequPagerAdapter;
import com.example.a28796.beautiful.stybean.FuliBean;
import com.example.a28796.beautiful.stybean.QuanziBean;
import com.example.a28796.beautiful.styfragment.FragmentHot;
import com.example.a28796.beautiful.styfragment.FragmentNew;
import com.example.a28796.beautiful.styfragment.FragmentNice;
import com.example.a28796.beautiful.stynet.NetConfig;
import com.example.a28796.beautiful.stytools.CubeTransformer;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 28796 on 2016/8/29.
 */
public class FragmentShequ extends Fragment {
    private RecyclerView mRecyclerView;
    private GridLayoutManager mGridLayoutManager;
    private ButtonAdapter mButtonAdapter;
    private List<FuliBean.ObjBean.ModulesBean> mButtonData;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private PagerAdapter mPagerAdapter;
    private BaseAdapter mAdapter;
    private List<Fragment> mFragments;
    private List<QuanziBean.ObjBean.ListDataBean> mData;
    private LinearLayout mLayout;
    private ImageView mImageViewMid,ivSearch,anim;
    private List<String> titles;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shequ,null);
        anim = (ImageView) view.findViewById(R.id.iv_anim_girl);
        AnimationDrawable drawable = (AnimationDrawable) anim.getBackground();
        drawable.start();
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(),SearchActivity.class));
            }
        });
    }

    private void setData() {
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setPageTransformer(true,new CubeTransformer());
        mTabLayout.setupWithViewPager(mViewPager);


        mGridLayoutManager = new GridLayoutManager(getActivity(),3, GridLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        mRecyclerView.setAdapter(mButtonAdapter);

    }

    private void initData() {
        mButtonData = new ArrayList<>();
        mButtonAdapter = new ButtonAdapter(mButtonData,getActivity());
        mFragments = new ArrayList<>();
        titles = new ArrayList<>();
        titles.add("热门");
        titles.add("精华");
        titles.add("最新");
        for (int i = 0; i < titles.size(); i++) {
            if (i == 0) {
                mFragments.add(new FragmentHot());
            } else if (i == 1) {
                mFragments.add(new FragmentNice());
            } else if (i == 2) {
                mFragments.add(new FragmentNew());
            }
        }
        mPagerAdapter = new ShequPagerAdapter(getFragmentManager(), mFragments, titles);
        mData = new ArrayList<>();
        mFragments = new ArrayList<>();

        x.http().get(new RequestParams(NetConfig.SHEQU_QUANZI), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                QuanziBean quanziBean = new Gson().fromJson(result, QuanziBean.class);
                final List<QuanziBean.ObjBean.ListDataBean> listData = quanziBean.getObj().getListData();
                for (int i = 0; i < listData.size(); i++) {
                    View scrollItem = View.inflate(getActivity(), R.layout.shequ_quanzi_scroll_item, null);
                    TextView textViewTitle = (TextView) scrollItem.findViewById(R.id.tv_quanzi_scroll_name);
                    TextView textViewCount = (TextView) scrollItem.findViewById(R.id.tv_quanzi_scroll_count);
                    textViewTitle.setTextColor(Color.BLACK);
                    textViewTitle.setTextSize(18);
                    textViewTitle.setText(listData.get(i).getName());
                    textViewCount.setText(listData.get(i).getBoardNum().getGroupNum() + "篇");
                    ImageView imageViewShow = (ImageView) scrollItem.findViewById(R.id.iv_quanzi_scroll_show);
                    Picasso.with(getActivity()).load(listData.get(i).getPhoto().getUrl()).into(imageViewShow);
                    final int finalI = i;
                    imageViewShow.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getActivity(),QuanziTwoActivity.class);
                            intent.putExtra("url",listData.get(finalI).getPhoto().getUrl());
                            intent.putExtra("name",listData.get(finalI).getName());
                            intent.putExtra("count",listData.get(finalI).getBoardNum().getGroupNum());
                            getActivity().startActivity(intent);
                        }
                    });
                    mLayout.addView(scrollItem);
                    anim.setVisibility(View.GONE);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        Picasso.with(getActivity()).load("http://fq1.img.meidaila.net/160811/FnbhYfBzBfD1tdK2mS2iMOI7lI_p.jpg").into(mImageViewMid);



        x.http().get(new RequestParams(NetConfig.SHEQU_HUODONGFULI), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                FuliBean fuliBean = new Gson().fromJson(result, FuliBean.class);
                mButtonData.addAll(fuliBean.getObj().getModules());
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView(View view) {
        ivSearch = (ImageView) view.findViewById(R.id.iv_shequ_show);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_shequ_show);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_shequ_show);
        mTabLayout = (TabLayout) view.findViewById(R.id.tbl_shequ_show);
        mLayout = (LinearLayout) view.findViewById(R.id.ll_shequ_quanzi);
        mImageViewMid = (ImageView) view.findViewById(R.id.iv_shequ_mid);
    }


}
